/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package pe.gob.mtc.dgac.licencias.controller;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import pe.gov.mtc.dgac.licencias.bean.AuditorTO;
import pe.gov.mtc.dgac.licencias.bean.AuditorTipoauditTO;
import pe.gov.mtc.dgac.licencias.service.AreaService;
import pe.gov.mtc.dgac.licencias.service.AuditorService;
import pe.gov.mtc.dgac.licencias.service.SeccionService;
import pe.gov.mtc.dgac.licencias.util.Utiles;

/**
 *
 * @author elvis
 */
@Controller
@Scope("session")
public class AuditorController {

    @Autowired
    private AuditorService auditorService;
    @Autowired
    private SeccionService seccionService;
    @Autowired
    private AreaService areaService;
    
    @RequestMapping("/calidad/lauditor.htm")
    public String lauditor(HttpServletRequest request,ModelMap model) {
        request.setAttribute("lauditor", auditorService.listarAuditores(new AuditorTO()));
        model.put("bauditor", new AuditorTO());
        System.out.println("calidad/lauditor");
        return "calidad/lauditor";
    }
    
    @RequestMapping("/calidad/bauditor.htm")
    public String bauditor(@Valid AuditorTO bauditor, BindingResult result,HttpServletRequest request,ModelMap model) {
        request.setAttribute("lauditor", auditorService.listarAuditores(bauditor));
        model.put("bauditor", bauditor);
        System.out.println("calidad/bauditor");
        return "calidad/lauditor";
    }
    
    @RequestMapping("/calidad/nauditor.htm")
    public String nauditor(HttpServletRequest request,ModelMap model) {
        System.out.println("calidad/nauditor");
        model.put("nauditor", new AuditorTO());
        return "calidad/nauditor";
    }
    
   
    @RequestMapping(value="/calidad/rauditor.htm",method=RequestMethod.POST)
    public String rauditor(@Valid AuditorTO nauditor, BindingResult result,HttpServletRequest request,ModelMap model) {
        try {
            auditorService.registrarAuditor(nauditor);
            System.out.println("calidad/rauditor");
            return lauditor(request, model);            
        } catch (Exception e) {
            model.put("nauditor", nauditor);
            model.put("msgError", e.getMessage());
            return "calidad/nauditor";
        }
    }
     
    @RequestMapping("/calidad/editAuditor.htm")
    public String editAuditor(@RequestParam("codigo") int id,HttpServletRequest request,ModelMap model) {
        System.out.println("calidad/nauditor");
        model.put("nauditor", auditorService.obtenerAuditor(id));
        return "calidad/nauditor";        
    }
     
    @RequestMapping("/calidad/verAuditor.htm")
    public String verAuditor(@RequestParam("codigo") int id,HttpServletRequest request,ModelMap model) {
        System.out.println("calidad/dauditor");
        model.put("dauditor",auditorService.obtenerAuditor(id));
        return "calidad/dauditor";        
    }

    /* =================================================================== **/
    
    
    @RequestMapping("/calidad/ldetauditor.htm")
    public String ldetauditor(HttpServletRequest request,ModelMap model, @RequestParam("codigo")int codigo) {
        AuditorTipoauditTO auditor = new AuditorTipoauditTO();
        auditor.setCodAuditor( auditorService.obtenerAuditor(codigo) );
        model.put("auditor", auditor);
        model.put("listadetaudit", auditorService.listarTipoAuditAuditor(codigo));
        System.out.println("calidad/ldetauditor & codigo = "+codigo);
        return "calidad/ldetauditor";
    }
    
   /* @RequestMapping(value="/calidad/bdetauditor.htm",method=RequestMethod.POST)
    public String bdetauditor(@Valid AuditorTipoauditTO auditor, BindingResult result, HttpServletRequest request,ModelMap model) {
        auditor.setCodAuditor( auditorService.obtenerAuditor(auditor.getCodAuditor().getCodAuditor().intValue()) );
        model.put("auditor", auditor);
        model.put("listadetaudit", auditorService.listarTipoAuditAuditor(auditor.getCodAuditor().getCodAuditor().intValue()));
        model.put("lcombosArea", areaService.listarAreas() );
        System.out.println("calidad/ldetauditor & codigo = "+codigo);
        return "calidad/ldetauditor";
    }*/
        
    @RequestMapping("/calidad/ndetauditor.htm")
    public String ndetauditor(HttpServletRequest request,ModelMap model, @RequestParam("codigo")int codigo) {
        AuditorTipoauditTO auditor = new AuditorTipoauditTO();
        auditor.setCodAuditor( auditorService.obtenerAuditor(codigo) );        
        model.put("ndetauditor", auditor );
        model.put("lcombosArea", areaService.listarAreas() );
        System.out.println("calidad/ldetauditor & codigo = "+codigo);
        return "calidad/ndetauditor";
    }
    
    @RequestMapping("/calidad/editDetAuditor.htm")
    public String editDetAuditor(HttpServletRequest request,ModelMap model, @RequestParam("codigo")int codigo) {
        AuditorTipoauditTO auditor = auditorService.obtenerDetuditor(codigo);
        model.put("ndetauditor", auditor );
        model.put("lcombosArea", areaService.listarAreas() );
        System.out.println("calidad/ldetauditor & codigo = "+codigo);
        return "calidad/ndetauditor";
    }
    
    @RequestMapping("/calidad/verDetAuditor.htm")
    public String verDetAuditor(HttpServletRequest request,ModelMap model, @RequestParam("codigo")int codigo) {
        AuditorTipoauditTO auditor = auditorService.obtenerDetuditor(codigo);
        model.put("ddetauditor", auditor );
        System.out.println("calidad/ddetauditor & codigo = "+codigo);
        return "calidad/ddetauditor";
    }
         
    @RequestMapping("/calidad/delDetAuditor.htm")
    public String delDetAuditor(HttpServletRequest request,ModelMap model, @RequestParam("codigo")int codigo, @RequestParam("c2")int c2) {
        auditorService.eliminarTipoAuditAuditor(codigo);
        return ldetauditor(request, model, c2);
    }
    
    @RequestMapping(value="/calidad/rdetauditor.htm",method=RequestMethod.POST)
    public String rdetauditor(@Valid AuditorTipoauditTO ndetauditor, BindingResult result,HttpServletRequest request,ModelMap model) {
         System.out.println("AQuiiiiii");
         try {
             ndetauditor.setFecInicio( Utiles.stringToDate( request.getParameter("fecInicio") , "dd/MM/yyyy") );
             ndetauditor.setFecTermino( Utiles.stringToDate( request.getParameter("fecTermino") , "dd/MM/yyyy"));
             
             auditorService.registrarTipoAuditAuditor(ndetauditor);
             
             System.out.println("calidad/ldetauditor");
             return ldetauditor(request, model, ndetauditor.getCodAuditor().getCodAuditor().intValue() );
         } catch (Exception e) {
             e.printStackTrace();
             model.put("ndetauditor", ndetauditor );
             model.put("lcombosArea", areaService.listarAreas() );
             return "calidad/ndetauditor";             
         }
        
    }
    
    @RequestMapping("/calidad/lAuditAuditor.htm")
    public String lAuditAuditor(@RequestParam("codigo") int id,HttpServletRequest request,ModelMap model) {
        System.out.println("calidad/dauditor");
        System.out.println("Lista de auditorias de este auditor");
        return "calidad/lauditor";
        
    }
 
    @RequestMapping("/calidad/delAuditor.htm")
    public String delAuditor(@RequestParam("codigo") int id,HttpServletRequest request,ModelMap model) {
        System.out.println("calidad/dauditor");
        auditorService.eliminarAuditor(""+id);
        model.put("mensaje", "elemento eliminado");
        return lauditor(request, model);
    }
    
    @RequestMapping(value = "/*/comboseccion.htm", method = RequestMethod.POST)
    public String comboSeccion(@RequestParam("cod") int cod,ModelMap model,HttpServletRequest request){
        request.getSession().setAttribute("lcombosSeccion", seccionService.listarSecciones(cod) );
        return "ajax/cmbSeccion";
    }
     
}
